package com.lgy.controller;

import com.lgy.beans.UserInfo;
import com.lgy.constant.CookieConstant;
import com.lgy.constant.RedisConstant;
import com.lgy.enums.ResultEnum;
import com.lgy.enums.RoleEnum;
import com.lgy.service.UserService;
import com.lgy.utils.CookieUtil;
import com.lgy.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.lgy.utils.ResultVOUtil.fail;
import static com.lgy.utils.ResultVOUtil.success;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 买家登录
     * @param openid
     * @param response
     * @return
     */
    @GetMapping("/buyer")
    public ResultVO buyer(@RequestParam("openid") String openid, HttpServletResponse response) {
        // 1.openid和数据库的数据匹配
        UserInfo userInfo = userService.findByOpenid(openid);
        if(userInfo == null) {
            return fail(ResultEnum.LOGIN_FAIL);
        }
        // 2.判断角色
        if(RoleEnum.BUYER.getCode() != userInfo.getRole()) {
            return fail(ResultEnum.ROLE_ERROR);
        }
        // 3.cookie里设置openid=abc
        CookieUtil.set(response, CookieConstant.OPENID, openid, CookieConstant.expire);
        return success();
    }

    /**
     * 卖家登录
     * @param openid
     * @param response
     * @return
     */
    @GetMapping("/seller")
    public ResultVO seller(@RequestParam("openid") String openid,
                           HttpServletRequest request,
                           HttpServletResponse response) {
        // 判断是否登录
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if(cookie != null && StringUtils.isNotEmpty(stringRedisTemplate.opsForValue()
                .get(String.format(RedisConstant.TOKEN_TEMPLATE, cookie.getValue())))) {
            return success();
        }

        // 1.openid和数据库的数据匹配
        UserInfo userInfo = userService.findByOpenid(openid);
        if(userInfo == null) {
            return fail(ResultEnum.LOGIN_FAIL);
        }
        // 2.判断角色
        if(RoleEnum.SELLER.getCode() != userInfo.getRole()) {
            return fail(ResultEnum.ROLE_ERROR);
        }
        // 3.redis设置key=UUID, value=xyz
        String token = UUID.randomUUID().toString();
        Integer expire = CookieConstant.expire;
        stringRedisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_TEMPLATE, token)
                , openid, expire, TimeUnit.SECONDS);
        // 4.cookie里设置token=UUID
        CookieUtil.set(response, CookieConstant.TOKEN, token, CookieConstant.expire);

        return success();
    }

}
